import 'dart:async';
import 'dart:convert';

import 'package:bruno/bruno.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:native_dart_package/http/request/exception/api_exception.dart';
import 'package:native_dart_package/loading/loading_util.dart';
import 'package:yt_devops/configs.dart';
import 'package:yt_devops/low_code/widgets/singleForm/models/low_code_single_form_analyzer.dart';
import 'package:yt_devops/low_code/widgets/singleForm/models/low_code_single_form_example.dart';

class LowCodeSingleFormController extends GetxController {
  LowCodeSingleFormAnalyzer state = LowCodeSingleFormAnalyzer.analyzeLocalModel(LowCodeSingleFormExample());
  @override
  void onInit() {
    super.onInit();
    initState();
  }

  void initState() async {
    // state = await _getItemWithType(Get.arguments['type'] ?? 1);
    // getFormData();
    state = await _getItemWithType(1);
    update();
  }

  //用来加载不同页面的model
  Future<LowCodeSingleFormAnalyzer> _getItemWithType(int type) async {
    switch (type) {
      case 0:
        return LowCodeSingleFormAnalyzer.analyzeLocalModel(LowCodeSingleFormExample());

      case 1:
        return LowCodeSingleFormAnalyzer.analyzeWebExample(
            await loadJsonFile('assets/json/single_form_web_example.json'));

      default:
        return LowCodeSingleFormAnalyzer.analyzeLocalModel(LowCodeSingleFormExample());
    }
  }

  Future<Map<String, dynamic>> loadJsonFile(String url) async {
    // 加载文件内容
    String jsonString = await rootBundle.loadString(url);

    // 解析JSON
    Map<String, dynamic> json = jsonDecode(jsonString);

    return json;
  }

  Future<void> getFormData() async {
    if (state.initDataConfig.serviceUrl.isEmpty) return;

    Map<String, dynamic> params = state.initDataConfig.serviceParam;
    showLoading();
    var result;
    onError(error) {
      dismissLoading();
      if (error is ApiException) {
        showError(error.message);
      } else {
        showError(error.toString());
      }
      return false;
    }

    // 请求数据
    if (state.initDataConfig.serviceMethod == 'get') {
      result = await Configs.httpClient.get(state.initDataConfig.serviceUrl, queryParameters: params, onError: onError);
    } else {
      result = await Configs.httpClient.post(state.initDataConfig.serviceUrl, data: params, onError: onError);
    }
    dismissLoading();
    if (result != null) {
      state.initDataList = result;
      initFormData();
    }
  }

  void initFormData() {}

  Future<void> confirmFormData() async {
    if (state.commitDataConfig.serviceUrl.isEmpty) return;

    Map<String, dynamic> params = state.commitDataConfig.serviceParam;
    showLoading();
    var result;
    onError(error) {
      dismissLoading();
      if (error is ApiException) {
        showError(error.message);
      } else {
        showError(error.toString());
      }
      return false;
    }

    // 请求数据
    if (state.commitDataConfig.serviceMethod == 'get') {
      result =
          await Configs.httpClient.get(state.commitDataConfig.serviceUrl, queryParameters: params, onError: onError);
    } else {
      result = await Configs.httpClient.post(state.commitDataConfig.serviceUrl, data: params, onError: onError);
    }
    dismissLoading();
    if (result != null) {
      showSuccessToast('保存成功');
    }
  }
}
